package com.unitedats.Project.Servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.unitedats.HR.Entity.HrEmployee;
import com.unitedats.Project.Entity.LookupCountry;
import com.unitedats.Project.Entity.LookupCurrency;
import com.unitedats.Project.Entity.PmProject;
import com.unitedats.Project.Entity.PmProjectMember;
import com.unitedats.Project.Entity.PmProjectPartner;
import com.unitedats.common.Servlet.BasicServlet;
import com.unitedats.common.bo.Message;
import com.unitedats.common.util.Common;
import com.unitedats.common.util.DBManager;
import com.unitedats.common.util.DateUtil;
import com.unitedats.common.util.FileUtil;
import com.unitedats.common.util.WebUtil;

@MultipartConfig
public class ProjectProcessServlet extends BasicServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		Integer projectId = WebUtil.getParamValueAsInteger(request, "pId", null);
		PmProject project = null;
		
		if (projectId != null) { // update mode
			project = dbm.findById(projectId, PmProject.class);
			project.setLastModifiedIn(DateUtil.getCurrentDate());
			project.setLastModifiedBy(getSessionUser(request).getEmpId());
		} else { // entry mode
			project = new PmProject();
			project.setCreatedIn(DateUtil.getCurrentDate());
			project.setCreatedBy(getSessionUser(request).getEmpId());
		}

		project.setName(WebUtil.getParamValue(request, "name", null));
		project.setCode(WebUtil.getParamValue(request, "code", null));

		Integer country = WebUtil.getParamValueAsInteger(request, "country", null);
		if (country != null) {
			LookupCountry countryObj = new LookupCountry();
			countryObj.setId(country);
			project.setCountryId(countryObj);
		}

		project.setTypeId(WebUtil.getParamValueAsInteger(request, "ptype", null));

		project.setStatusId(WebUtil.getParamValueAsInteger(request, "status", null));

		project.setPlannedStartDate(WebUtil.getParamValueAsDate(request, "plannedStartDate", null));
		project.setPlannedEndDate(WebUtil.getParamValueAsDate(request, "plannedEndDate", null));
		project.setActualStartDae(WebUtil.getParamValueAsDate(request, "actualStartDate", null));
		project.setActualEndDate(WebUtil.getParamValueAsDate(request, "actualEndDate", null));

		if (project.getPmProjectPartnerList() == null)
			project.setPmProjectPartnerList(new ArrayList<PmProjectPartner>());
		else
			project.getPmProjectPartnerList().clear();

		if (WebUtil.getParamValue(request, "parteners_rowOrder", null) != null) {
			String[] partnersOrders = WebUtil.getParamValue(request, "parteners_rowOrder", "").split(",");
			for (int i = 0; i < partnersOrders.length; i++) {
				PmProjectPartner partner = new PmProjectPartner();
				partner.setName(WebUtil.getParamValue(request, "parteners_name_" + partnersOrders[i], null));
				LookupCountry c2 = new LookupCountry();
				c2.setId(WebUtil.getParamValueAsInteger(request, "parteners_country_" + partnersOrders[i], null));
				partner.setCountryId(c2);
				project.addPmProjectPartner(partner);
			}
		}

		if (project.getPmProjectMemberList() == null)
			project.setPmProjectMemberList(new ArrayList<PmProjectMember>());
		else
			project.getPmProjectMemberList().clear();

		if (WebUtil.getParamValue(request, "teamwork_rowOrder", null) != null) {
			String[] membersOrders = WebUtil.getParamValue(request, "teamwork_rowOrder", "").split(",");
			for (int i = 0; i < membersOrders.length; i++) {
				PmProjectMember member = new PmProjectMember();

				HrEmployee e = new HrEmployee();
				e.setId(WebUtil.getParamValueAsInteger(request, "teamwork_name_" + membersOrders[i], null));
				member.setEmpId(e);
				member.setRole(WebUtil.getParamValue(request, "teamwork_role_" + membersOrders[i], null));
				project.addPmProjectMember(member);
			}
		}

		project.setTotalContractValue(WebUtil.getParamValueAsFloat(request, "totalContractValue", null));

		if (WebUtil.getParamValue(request, "currency", null) != null) {
			LookupCurrency cu = new LookupCurrency();
			cu.setId(WebUtil.getParamValueAsInteger(request, "currency", null));
			project.setContractCurrentcyId(cu);
		}

		project.setContractFileUrl(Common.concatenateValues(FileUtil.uploadFiles(request, "contractFileUrl")));
		project.setPoUrl(Common.concatenateValues(FileUtil.uploadFiles(request, "poUrl")));

		project.setContractNo(WebUtil.getParamValue(request,"contractNumber",null));

		project = dbm.mergeEntity(project);

		if (projectId == null) {
			setMessage(request, new Message("Project added successfully", Message.MESSAGE_TYPE.SUCCESS));
			sendRedirect(request, response, "ProjectDisplay");

		} else {
			setMessage(request, new Message("Project updated successfully", Message.MESSAGE_TYPE.SUCCESS));
			sendRedirect(request, response, "SearchForProjectDisplay");
		}

	}
}
